package ihm.views.help;

import ihm.actions.CloseFrameAction;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

import systems.configurations.SystemConfiguration;
import systems.languages.MultiLanguages;

public class AboutBox extends JDialog{

  private static final long serialVersionUID = 1L;

  private static LayoutManager gridBagLayout = new GridBagLayout();
  private static GridBagConstraints  gbc = new GridBagConstraints();
  private static Properties systemProperties = SystemConfiguration.getProperties();
  private static String boldPrefix = "<html><b>";
  private static String boldSuffix = "</b></html>";
  
  public AboutBox()
  {
    super();
    this.setLayout(gridBagLayout);
    this.setResizable(false);
    this.setModal(true);
    this.setTitle(MultiLanguages.getProperties("dialog.AboutBox.title"));
    this.buildIcon();
    this.buildHeader();
    this.buildText();
    this.buildVersion();
    this.buildCreators();
    this.buildHomepage();
    this.buildCopyRight();
    this.buildClose();
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.pack();
    this.setVisible(true);
  }



  private void buildIcon()
  {
    JLabel iconLabel = new JLabel();
    iconLabel.setIcon(new ImageIcon(MultiLanguages.getProperties("dialog.AboutBox.icon")));
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = GridBagConstraints.REMAINDER;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.CENTER;
    gbc.anchor = GridBagConstraints.CENTER;
    add(iconLabel, gbc);
  }
  
  private void buildHeader()
  {
    String prefix = "<html><h2>";
    String sufix = "</h2></html>";
    JLabel header = new JLabel(prefix+systemProperties.getProperty("program.Name")+sufix);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    gbc.gridwidth =  GridBagConstraints.REMAINDER;
    gbc.anchor = GridBagConstraints.CENTER;
    add(header, gbc);
  }
  
  private void buildText()
  {
    String prefix = "<html>";
    String sufix = "</html>";
    JLabel text = new JLabel(prefix+MultiLanguages.getProperties("dialog.AboutBox.text").replaceAll("\\$n", "<br/>")+sufix);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth =  GridBagConstraints.REMAINDER;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(text, gbc);
  }
  
  private void buildVersion()
  {
    JLabel versionLabel = new JLabel(boldPrefix+MultiLanguages.getProperties("dialog.AboutBox.version")+boldSuffix);
    JLabel versionNumber = new JLabel(systemProperties.getProperty("program.Version"));
    //Label
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth =  1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(versionLabel, gbc);
    //Version
    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth =  1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(versionNumber, gbc);
  }
  
  private void buildCreators()
  {
    JLabel creatorsLabel = new JLabel(boldPrefix+MultiLanguages.getProperties("dialog.AboutBox.creators")+boldSuffix);
    JLabel creatorsName = new JLabel(systemProperties.getProperty("program.Creators"));
    //Label
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(creatorsLabel, gbc);
    //Version
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(creatorsName, gbc);
  }
  
  private void buildHomepage()
  {
    JLabel homepageLabel = new JLabel(boldPrefix+MultiLanguages.getProperties("dialog.AboutBox.homepage")+boldSuffix);
    JLabel homepageAddress = new JLabel(systemProperties.getProperty("program.Homepage"));
    //Label
    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.gridheight = 1;
    gbc.gridwidth =  1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(homepageLabel, gbc);
    //Version
    gbc.gridx = 2;
    gbc.gridy = 4;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(homepageAddress, gbc);
  }
  
  private void buildCopyRight() {
    JLabel homepageLabel = new JLabel(boldPrefix+MultiLanguages.getProperties("dialog.AboutBox.copyright")+boldSuffix);
    JLabel homepageAddress = new JLabel("Feel free to use and redistribute");
    //Label
    gbc.gridx = 1;
    gbc.gridy = 5;
    gbc.gridheight = 1;
    gbc.gridwidth =  1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(homepageLabel, gbc);
    //Version
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    add(homepageAddress, gbc);
  }
  
  private void buildClose()
  {
    JButton close = new JButton(MultiLanguages.getProperties("button.Close.label"));
    close.addActionListener(new CloseFrameAction(this));
    gbc.gridx = 2;
    gbc.gridy = 6;
    gbc.gridheight = 1;
    gbc.gridwidth =  GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.LINE_END;
    gbc.anchor = GridBagConstraints.LINE_END;
    add(close, gbc);
  }
}
